import {GET,POST} from "./config"
import axios from "axios";



const instance = axios.create({
  baseURL:"https://api.it120.cc",//发送请求时，会在url前拼接baseUrl
  timeout:10000,
  headers:{'Content-Type':'application/x-www-form-urlencoded'},

   transformRequest:[function (data) {
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }]
})


export function request(method,url,params){
  switch (method){
    case GET:
      return get(url,params)
    case POST:
      return post(url,params)
  }
}

function get(url,params){
  return instance.get(url,params)
}

function post(url,params){
  return instance.post(url,params)
}


instance.interceptors.request.use((config) => {
  // 预处理请求的信息
  console.log('拦截器');
  
  return config
}, (error) => {
  // 预处理请求有异常error时抛出错误
  return Promise.reject(error)
})
 
/*
* 增加相应拦截器
*/
instance.interceptors.response.use((response) => {
  // 预处理相应的数据
  console.log('拦截2器');
  
  return response
}, (error) => {
  // 错误返回 状态码验证
  return Promise.reject(error)
})
